package com.oracle.malldemo.servlet.address;


import com.alibaba.fastjson.JSONObject;
import com.oracle.malldemo.domain.Address;
import com.oracle.malldemo.domain.Catalog;
import com.oracle.malldemo.view.JsonData;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet(value="/address/list")
public class QueryAddressListServlet extends AddressServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/plain;charset=utf-8");
        resp.setCharacterEncoding("utf-8");
        PrintWriter out = resp.getWriter();
        List<Address> addressList;
        try {
            String uid = req.getParameter("uid");
            addressList = addressService.queryAddressListByUid(Integer.parseInt(uid));
            JsonData result = JsonData.success("查询收货地址集合成功！", addressList);
            out.write(JSONObject.toJSONString(result));
            out.flush();
            out.close();
        } catch (Exception ex) {
            ex.printStackTrace();
            JsonData result = JsonData.fail("程序出现异常！");
            out.write(JSONObject.toJSONString(result));
            //out.write(result.toString());
            out.flush();
            out.close();
        }
    }
}
